package com.sisbibvir.unmsm.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sisbibvir.unmsm.entidades.Tesi;

public class TesisDAO extends DaoGenericImpl<Tesi, Integer> {

	public TesisDAO(EntityManager entityManager) {
		super(entityManager);
	}

	@SuppressWarnings("unchecked")
	public List<Tesi> ListarBusqueda(String sBusqueda) {
		List<Tesi> aux = null;
		try {
			entityManager.getTransaction().begin();
			if (sBusqueda.equalsIgnoreCase("")) {
				aux = entityManager.createQuery("SELECT p FROM Tesi p ")
						.getResultList();

			} else {
				Query query = entityManager
						.createQuery("SELECT p FROM Tesi p WHERE (p.titulo LIKE '%"
								+ sBusqueda
								+ "%') OR (p.tipo LIKE '%"
								+ sBusqueda + "%')");
				aux = query.getResultList();
			}
			entityManager.getTransaction().commit();
		} catch (Exception e) {
			System.out.println("Error en TesisDAO:ListarBusqueda"
					+ e.getMessage());
		}

		return aux;
	}

	public List<Tesi[]> listarTesis(String entrada) {
		entityManager.getTransaction().begin();
		@SuppressWarnings("unchecked")
		/*
		 * List<Tesi> aux = entityManager.createQuery(
		 * "select t.id, t.titulo, t.resumen, t.tipo, t.archivo , t.estado from tesis t "
		 * + "inner join detalle_tags_tesis dtt on t.id=dtt.tesis_id " +
		 * "inner join tags_tesis tt on dtt.Tags_Tesis_id=tt.id where tt.descripcion='"
		 * +entrada+"'").getResultList();
		 */
		List<Tesi[]> aux = (List<Tesi[]>) entityManager
				.createNativeQuery(
						"select t.id, t.titulo, t.resumen, t.tipo, t.archivo , t.estado from tesis t "
								+ "inner join detalle_tags_tesis dtt on t.id=dtt.tesis_id "
								+ "inner join tags_tesis tt on dtt.Tags_Tesis_id=tt.id where tt.descripcion='"
								+ entrada + "'").getResultList();

		entityManager.getTransaction().commit();
		return aux;
	}

	public Tesi getTesis(int id) {
		Tesi tesis = new Tesi();
		try {
			tesis = entityManager.find(
					com.sisbibvir.unmsm.entidades.Tesi.class, id);
		} catch (Exception e) {
			System.out.println("Error en GetTEsis TEsisDAO: " + e.getMessage());
		}
		return tesis;
	}

	public int GenerarCodigo() {
		int iNuevoCodigo = 1;
		try {
			Query q = entityManager.createQuery("Select max(t.id) from Tesi t");
			iNuevoCodigo = Integer.parseInt(q.getSingleResult().toString()) + 1;
			System.out.println("Codigo generado: " + iNuevoCodigo);
		} catch (Exception e) {
			System.out.println("Error en el TEsisDAO GenerarCodigo "
					+ e.getMessage());
		}
		return iNuevoCodigo;
	}

	public List<Tesi> BuscarPorLinea(int id) {
		List<Tesi> tesis = null;
		try {
			entityManager.getTransaction().begin();
			Query q = entityManager
					.createQuery("SELECT DISTINCT lt.tesi FROM Lineatesi lt INNER JOIN LineaDeInvestigacion l WHERE lt.lineaDeInvestigacion.id="
							+ id);
			tesis = q.getResultList();
			System.out.println("Numero de tesis en Buscarpor Linea : "
					+ tesis.size());
			entityManager.getTransaction().commit();
		} catch (Exception e) {
			System.out.println("Error en el TEsisDAO BuscarPorLinea"
					+ e.getMessage());
		}
		return tesis;
	}

}
